package net.lzzy.practices.web.course;

import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Chapter;
import net.lzzy.practices.services.ChapterService;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author  Jaymo on 2022/5/30
 */
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROUTE_CHAPTER_ROOT)
public class ChapterApiController {
    private  final ChapterService service;

    @Autowired
    public  ChapterApiController(ChapterService service){
        this.service = service;
    }

    @GetMapping(ApiConstants.ROUTE_CHAPTER_TEACHER)
    public ApiResult getTeachersChapters(@RequestParam String token, @RequestParam(name = "id")UUID courseId){
        List<Chapter> chapters = service.getTeachersChapters(courseId,token);
        return  transform(chapters);
    }

    private  ApiResult transform(List<Chapter> chapters){
        if (chapters == null){
            return  new ApiResult(false, Messages.WRING_ID + "或" + Messages.No_PERMISSION, null);
        }
        List<VmChapter> vmChapters = new ArrayList<>();
        chapters.forEach(chapter -> vmChapters.add(new VmChapter(chapter)));
        return  new ApiResult(true, vmChapters.size() + "", vmChapters);
    }

    @GetMapping(ApiConstants.ROUTE_CHAPTER_STUDENT)
    public  ApiResult getStudentsChapters(@RequestParam String token, @RequestParam(name = "id") UUID courseId){
        List<Chapter> chapters = service.getStudentsChapters(courseId,token);
        return  transform(chapters);
    }

    private  ApiResult addOrUpdate(UUID id, ChapterInfo info){
       String token = info.getToken();
       String name = info.getName();
       String desc = info.getDesc();
       String strCourseId = info.getCourseId();
       boolean open = info.isOpen();
       if (StringUtils.isEmpty(token) ||
          StringUtils.isEmpty(name)||
       StringUtils.isEmpty(desc) ||
       StringUtils.isEmpty(strCourseId)){
           return  new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
       }
       boolean create = id == null;
       UUID courseId = UUID.fromString(strCourseId);
       Chapter chapter = new Chapter();
       chapter.setOpen(open);
       chapter.setName(name);
       chapter.setDescription(desc);
       VmChapter vmChapter;
       if (create){
           vmChapter = new VmChapter(service.add(courseId,chapter,token));

       }else {
           vmChapter = new VmChapter(service.update(id,chapter,token));
       }
       return  new ApiResult(true,Messages.SUCCESS.toString(), vmChapter);
    }

    @PostMapping(ApiConstants.ROUTE_CHAPTER_ADD)
    public  ApiResult add(@PathVariable UUID id,@RequestBody ChapterInfo info){
        return  addOrUpdate(null, info);
    }

    @PostMapping(ApiConstants.ROUTE_CHAPTER_UPDATE)
    public  ApiResult update(@PathVariable UUID id, @RequestBody ChapterInfo info){
        return  addOrUpdate(id,info);
    }

    @PostMapping(ApiConstants.ROUTE_CHAPTER_OPEN_CLOSE)
    public  ApiResult openCloseChapter(@RequestBody ChapterOpenInfo info){
        String token = info.getToken();
        String strChapterId = info.getChapterId();
        boolean open = info.isOpen();

        if (StringUtils.isEmpty(token)||StringUtils.isEmpty(strChapterId)){
            return  new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
        }
        Chapter chapter = service.openCloseChapter(UUID.fromString(strChapterId),open,token);
        if (chapter == null){
            return  new ApiResult(false,Messages.WRING_ID + "或" +Messages.No_PERMISSION,null);
        }
        return  new ApiResult(true,Messages.SUCCESS.toString(),new VmChapter(chapter));
    }
    @DeleteMapping(ApiConstants.ROUTE_CHAPTER_REMOVE)
    public ApiResult delete(@RequestBody ChapterKeyInfo info){
        String token = info.getToken();
        String strChapterId = info.getChapterId();
        if (StringUtils.isEmpty(token)|| StringUtils.isEmpty(strChapterId)){
            return  new ApiResult(false,Messages.INCOMPLETE_INFO.toString(),null);
        }
        Messages msg = service.remove(UUID.fromString(strChapterId),token);
        return  new ApiResult(msg == Messages.SUCCESS, msg.toString(), null);
    }

}
